/*
 * Copyright (C) 2010 Yury Kudryashov.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package name.kudryashov.functions;

import name.kudryashov.base.Argument;

public class ComposableProcedure<A> implements Procedure<A> {
    private final Procedure<A> f;

    public ComposableProcedure(Procedure<A> f) {
        this.f = Argument.requireNotNull(f, "f");
    }

    public void call(A arg) {
        f.call(arg);
    }

    public Runnable bind(final A arg) {
        return new Runnable() {
            public void run() {
                f.call(arg);
            }

            public String toString() {
                return f + "(" + arg + ")";
            }
        };
    }

    public <R> Function<A, R> returnValue(final R result) {
        return new Function<A, R>() {
            public R apply(A arg) {
                f.call(arg);
                return result;
            }

            public String toString() {
                return f + "->" + result;
            }
        };
    }

    public <R> Function<A, R> returnValue(final Generator<R> resultF) {
        return new Function<A, R>() {
            public R apply(A arg) {
                f.call(arg);
                return resultF.get();
            }

            public String toString() {
                return f + "->" + resultF;
            }
        };
    }
}
